package com.example.it.looker.view.customview;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

/**
 * Created by 鲁迅认识的那只猹 on 9/4/2017 8:57 AM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public class LoopViewPager extends ViewPager {

    public LoopViewPager(Context context) {
        super(context);
        init();
    }

    public LoopViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 初始化ViewPager
     */
    private void init() {
        this.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            /**
             * 在这里实现无限循环
             */
            @Override
            public void onPageSelected(int position) {
                if (position == 0)
                    setCurrentItem(getAdapter().getCount() - 2);
                if (position == getAdapter().getCount() - 1)
                    setCurrentItem(1, false);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


}
